var reverseWords = function (s) {
  s = s.trim();
  let str = [...s];
  let res = [];
  let temp = "";
  for (let i = 0; i < str.length; i++) {
    if (str[i] === " ") {
      if (temp.trim().length) {
        res.unshift(temp);
        temp = "";
      }
    } else {
      temp += str[i];
    }
  }
  res.unshift(temp);
  return res.join(" ");
}